﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using BitPortal.Models;
using System.Web.Security;

namespace BitPortal.Business
{
    public partial class BitContext
    {
       
        //Current Language
        public Models.Language Lang
        {
            get
            {
                //Check from Querystring
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["lang"]))
                {
                    Language lang = LanguageBLL.GetByLanguageCode(HttpContext.Current.Request.QueryString["lang"]);
                    if (lang != null && lang.IsEnabled)
                    {
                        return lang;
                    }
                }
                //Check from Session
                if (HttpContext.Current.Session["lang"] != null)
                {
                    return (Language)HttpContext.Current.Session["lang"];
                }
                //Return default Language
                Language defaultLang = LanguageBLL.GetDefault();
                HttpContext.Current.Session["lang"] = defaultLang;
                return (defaultLang);
            }
        }

        //Current Theme
        public string Theme
        {
            get
            {
                //Get from Session
                if (HttpContext.Current.Session["Theme"] != null)
                {
                    return (string)HttpContext.Current.Session["Theme"];
                }
                //Get Default theme
                string theme = (string)PortalConfigBLL.GetValueByName("Theme");
                HttpContext.Current.Session["Theme"] = theme;
                return theme;
            }
        }

        public Guid? LoginUser
        {
            get
            {
                return (Membership.GetUser() == null) ? null : (Guid?)(new Guid(Membership.GetUser().ProviderUserKey.ToString()));
            }
        }
        
        public static BitContext Current
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    object data = Thread.GetData(Thread.GetNamedDataSlot("BitContext"));
                    if (data != null)
                    {
                        return (BitContext)data;
                    }
                    BitContext context = new BitContext();
                    Thread.SetData(Thread.GetNamedDataSlot("BitContext"), context);
                    return context;
                }
                if (HttpContext.Current.Items["BitContext"] == null)
                {
                    BitContext context = new BitContext();
                    HttpContext.Current.Items.Add("BitContext", context);
                    return context;
                }
                return (BitContext)HttpContext.Current.Items["BitContext"];
            }
        }

    }
}